package com.lyw.spring.demo.controller;

import com.lyw.spring.demo.model.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/resp")
@Controller//返回页面
public class RespController {

    //返回页面
    @RequestMapping("/r1")
    public String returnPage() {
        return "/calc.html";
    }

    @RequestMapping("/r2")
    public String returnPage2() {
        return "/aa/calc.html";
    }

    //返回数据
    @RequestMapping("/r3")
    @ResponseBody
    public String retuenDate() {
        return "前端数据";
    }

    //返回html片段
    @RequestMapping("/r4")
    @ResponseBody
    public String retuenHTML() {
        return "<h1>我是一级标题</h1>";
    }

    //不想返回html返回文本
    @RequestMapping(value = "/r5", produces = "text/plain")
    @ResponseBody
    public String returnText() {
        return "<h1>我返回的是文本不是html</h1>";
    }

    //返回json数据 对象 数据结构一些东西东json
    @RequestMapping("/r6")
    @ResponseBody
    public UserInfo returnJson() {
        return new UserInfo("张三", 18, "男");
    }

    //返回状态码
    @RequestMapping("/r7")
    @ResponseBody
    public UserInfo returnStatus(HttpServletResponse response) {
        response.setStatus(404);
        //请求的成功与否与状态码值无关(状态码报错信息的原因)
        return new UserInfo("张三", 18, "男");
    }

    //设置返回header
    @RequestMapping("/r8")
    @ResponseBody
    public String returnHeader(HttpServletResponse response) {
        response.setHeader("myHeader", "666");
        return "设置header成功";
    }
}
